aboutsummaryrefslogtreecommitdiff
path: root/src/routes/grocery/[id]/+page.server.ts
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-07-28 15:29:32 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-07-28 15:29:32 +0200
commitdb2f142aaf98ba36d1e9b17ce8a4299268d1b69b (patch)
treeffe4e6d5d2c25e26118426627203971b06467ef7 /src/routes/grocery/[id]/+page.server.ts
parent04fabf115ccea31ac504b06cc51d78423b697f60 (diff)
feat: editable name and category in grocery detail page
Diffstat (limited to 'src/routes/grocery/[id]/+page.server.ts')
-rw-r--r--src/routes/grocery/[id]/+page.server.ts23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/routes/grocery/[id]/+page.server.ts b/src/routes/grocery/[id]/+page.server.ts
index 3c3967a..cf8a949 100644
--- a/src/routes/grocery/[id]/+page.server.ts
+++ b/src/routes/grocery/[id]/+page.server.ts
@@ -2,23 +2,25 @@ import { error, fail, redirect } from '@sveltejs/kit';
import { db } from '$lib/server/db';
import { groceries, categories } from '$lib/server/db/schema';
import { eq } from 'drizzle-orm';
+import { loadAll as loadCategories } from '$lib/server/services/categories';
import {
loadForGrocery,
add as addRef,
deleteReference,
} from '$lib/server/services/references';
-import { deleteGrocery } from '$lib/server/services/groceries';
+import { updateGrocery, deleteGrocery } from '$lib/server/services/groceries';
import {
numericId,
parseFormString,
parseOptionalNumber,
} from '$lib/server/services/validation';
-import type { Grocery, Reference } from '$lib/server/services/types';
+import type { Category, Grocery, Reference } from '$lib/server/services/types';
import type { Actions, PageServerLoad } from './$types';
export interface GroceryDetailOutput {
item: Grocery & { categoryName: string | null };
references: Reference[];
+ categories: Category[];
}
export const load: PageServerLoad = async ({ params }) => {
@@ -35,6 +37,7 @@ export const load: PageServerLoad = async ({ params }) => {
: [null];
const refs = await loadForGrocery(id);
+ const cats = await loadCategories();
return {
item: {
@@ -42,6 +45,7 @@ export const load: PageServerLoad = async ({ params }) => {
categoryName: cat?.name ?? null,
},
references: refs,
+ categories: cats,
};
};
@@ -73,6 +77,21 @@ export const actions: Actions = {
return { success: true };
},
+ update: async ({ request, params }) => {
+ const data = await request.formData();
+ const groceryId = numericId(params.id);
+ const name = parseFormString(data.get('name'));
+ const rawCategoryId = parseOptionalNumber(data.get('category_id'));
+ const categoryId = rawCategoryId === -1 ? null : rawCategoryId;
+
+ if (!name) {
+ return fail(400, { error: 'Le nom est requis' });
+ }
+
+ await updateGrocery(groceryId, name, categoryId);
+ return { success: true };
+ },
+
deleteItem: async ({ params }) => {
const groceryId = numericId(params.id);
await deleteGrocery(groceryId);